var express = require('express');
let DB = require("../tools/DB");
var router = express.Router();
let md5 = require("md5");

/* GET home page. */
router.get('/', function(req, res, next) {
  let secret = "offcn.com"
  res.send( md5("11212") )
});

router.get("/login",(req,res)=>{
  res.render("login.ejs");
})
router.get("/register",(req,res)=>{
  res.render("register.ejs");
})
// 响应注册
router.post("/doregister",async (req,res,next)=>{
  let {username,password} = req.body;
  // 判断username数据库中是否存在
  let sql1 = "select * from users where username=?";
  let b = await DB.query(sql1,[username]);
  if(b.length > 0){
    // res.send("该用户已注册");
    next("该用户已注册");
    return
  }
  let sql = "insert into users(username,password) values(?,?)";
  let a =await DB.query(sql,[username,password]);
  if(a.affectedRows>0){
    res.send("注册成功")
  }else{
    res.send("注册失败")
  }
})
// 响应登录
router.post("/dologin",async (req,res)=>{
 
})


module.exports = router;
